package com.chatSystem2;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.net.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Server {
    static ServerSocket server = null;
    static Socket socket = null;
    static List<Socket> list = new ArrayList<Socket>();//存客户端
    static Socket[] user = new Socket[10];
    public static int[] haveUser = new int[10];


    public static Socket[] getSocket(){
        return user;
    }


    public static void main(String[] args) {

        ServerWindow serverWindow = new ServerWindow();//新建聊天服务器界面
        try {
            server = new ServerSocket(8081);//服务器端套接字

            //等待连接，连接后开启线程
            while(true){
                socket = server.accept();//等待连接
                list.add(socket);//添加当前客户端到列表
                int x = list.size();
                System.out.println(list.get(x-1));
                int i = 0;
                user[i] = list.get(x-1);

                //在服务器为客户端开启线程
                com.chatSystem2.ServerReadAndPrint readAndPrint = new com.chatSystem2.ServerReadAndPrint(socket,serverWindow);
                readAndPrint.start();
            }
        }catch (IOException e){
            e.printStackTrace();
        }

    }
}




class ServerReadAndPrint extends Thread{
    Socket thisSocket = null;
    ServerWindow serverWindow = null;
    BufferedReader bufferedReader = null;
    BufferedWriter bufferedWriter = null;
    PrintWriter out = null;
    JButton Xuan = ServerWindow.getXuan();
    JButton Tao = ServerWindow.getTao();
    JButton Yuan = ServerWindow.getYuan();
    static Socket[] user= new Socket[10];
    public static int[] haveUser = new int[10];
    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public ServerReadAndPrint(Socket s, ServerWindow serverWindow) {
        super();
        this.serverWindow = serverWindow;  // 获取多人聊天系统界面
        this.thisSocket = s;  // 获取当前客户端信息
    }
    public ServerReadAndPrint(){}

    class serverWindowListen implements ActionListener{
        Socket[] user = com.chatSystem2.Server.getSocket();
        BufferedWriter bw;

        public void actionPerformed(ActionEvent event) {
            JButton jbuttonTemp = (JButton) event.getSource();
            if(jbuttonTemp == Xuan){
                String s1 = "Server message:You have been logged out by Server，message will not send to other clients @xuan";
                System.out.println("获取动作成功！");
                haveUser[0] = 0;
                try {
                    for(Socket socket: com.chatSystem2.Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        out.println(simpleDateFormat.format(new Date()) + s1);
                        out.flush();
                    }



                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(jbuttonTemp == Tao){
                String s2 = "Server message:You have been logged out by Server，message will not send to other clients @tao";
                System.out.println("获取动作成功！");
                haveUser[1] = 0;
                try {
                    for(Socket socket: com.chatSystem2.Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        out.println(simpleDateFormat.format(new Date()) + s2);
                        out.flush();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
            if(jbuttonTemp == Yuan){
                String s3 = "Server message:You have been logged out by Server，message will not send to other clients @yuan";
                System.out.println("获取动作成功！");
                haveUser[2] = 0;
                try {
                    for(Socket socket: com.chatSystem2.Server.list) {
                        bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                        out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                        out.println(simpleDateFormat.format(new Date()) + s3);
                        out.flush();
                    }


                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }

    public void run(){
        JButton Xuan = ServerWindow.getXuan();
        JButton Tao = ServerWindow.getTao();
        JButton Yuan = ServerWindow.getYuan();
        try{
            bufferedReader = new BufferedReader(new InputStreamReader(thisSocket.getInputStream()));//输入流

            while (true) {
                String str = bufferedReader.readLine();
                if (str.contains("User[xuan]has connected to Server")) {
                    haveUser[0] = 1;
                    Xuan.setEnabled(true);
                }
                if (str.contains("User[xuan]has disconnected from Server")) {
                    haveUser[0] = 0;
                    Xuan.setEnabled(false);
                }
                if (str.contains("User[tao]has connected to Server")) {
                    haveUser[1] = 1;
                    Tao.setEnabled(true);
                }
                if (str.contains("User[tao]has disconnected from Server")) {
                    haveUser[1] = 0;
                    Tao.setEnabled(false);
                }
                if (str.contains("User[yuan]has connected to Server")) {
                    haveUser[2] = 1;
                    Yuan.setEnabled(true);
                }
                if (str.contains("User[yuan]has disconnected from Server")) {
                    haveUser[2] = 0;
                    Yuan.setEnabled(false);
                }
                for(Socket socket: Server.list) {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                    if(haveUser[0] == 1){
                        out.println(simpleDateFormat.format(new Date()) + "friend[xuan]has logged in");
                        out.flush();
                    }
                    if(haveUser[1] == 1){
                        out.println(simpleDateFormat.format(new Date()) + "friend[tao]has logged in");
                        out.flush();
                    }
                    if(haveUser[2] == 1){
                        out.println(simpleDateFormat.format(new Date()) + "friend[yuan]has logged in");
                        out.flush();
                    }
                    if(haveUser[0] == 0){
                        out.println(simpleDateFormat.format(new Date()) + "friend[xuan]has logged out");
                        out.flush();
                    }
                    if(haveUser[1] == 0){
                        out.println(simpleDateFormat.format(new Date()) + "friend[tao]has logged out");
                        out.flush();
                    }
                    if(haveUser[2] == 0){
                        out.println(simpleDateFormat.format(new Date()) + "friend[yuan]has logged out");
                        out.flush();
                    }
                }

                System.out.println(str);

                // 发送给所有客户端
                for(Socket socket: Server.list) {
                    bufferedWriter = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                    out = new PrintWriter(socket.getOutputStream());  // 对每个客户端新建相应的socket套接字
                    if (socket == thisSocket) {  // 发送给当前客户端
                        out.println(simpleDateFormat.format(new Date()) + "(you)" + str);
                    }
                    else {  // 发送给其它客户端
                        out.println(simpleDateFormat.format(new Date()) + str);
                    }
                    out.flush();  // 清空out中的缓存
                }
                // 调用自定义函数输出到图形界面
                serverWindow.setTextArea(str);
            }
        } catch (Exception e) {
            Server.list.remove(thisSocket);  // 线程关闭，移除相应套接字
        }
    }

}
